/**
 * 获取指定日期的周的第一天、月的第一天、季的第一天、年的第一天
 * @param date new Date()形式，或是自定义参数的new Date()
 * @returns 返回值为格式化的日期，yy-mm-dd
 */
//日期格式化，返回值形式为yy-mm-dd
function timeFormat(date) {
  if (!date || typeof date === "string") {
    this.error("参数异常，请检查...");
  }
  var y = date.getFullYear(); //年
  var m = date.getMonth() + 1; //月
  var d = date.getDate(); //日

  return y + "-" + m + "-" + d;
}
//获取当前倒数第二天
function getNextToLastDay(date) {
  if (!date || typeof date === "string") {
    this.error("参数异常，请检查...");
  }
  date = new Date(date.getTime() - 1000 * 60 * 60 * 24);
  var y = date.getFullYear(); //年
  var m = date.getMonth() + 1; //月
  var d = date.getDate(); //日

  return y + "-" + m + "-" + d;
}
//获取当前时间
function getNowDay(date) {
  if (!date || typeof date === "string") {
    this.error("参数异常，请检查...");
  }
  var y = date.getFullYear(); //年
  var m = date.getMonth() + 1; //月
  var d = date.getDate(); //日
  //  var h = date.getHours()
  //   var m = myDate.getMinutes(); //获取分
  //  var s = myDate.getSeconds(); //获取秒

  return y + "-" + m + "-" + d;
}
//获取这周的周一
function getFirstDayOfWeek(date) {
  var weekday = date.getDay() || 7; //获取星期几,getDay()返回值是 0（周日） 到 6（周六） 之间的一个整数。0||7为7，即weekday的值为1-7

  date.setDate(date.getDate() - weekday + 1); //往前算（weekday-1）天，年份、月份会自动变化
  return timeFormat(date);
}

//获取当月第一天
function getFirstDayOfMonth(date) {

  date.setDate(1);
  return timeFormat(date);
}

//获取当季第一天
function getFirstDayOfSeason(date) {
  var month = date.getMonth();
  if (month < 3) {
    date.setMonth(0);
  } else if (2 < month && month < 6) {
    date.setMonth(3);
  } else if (5 < month && month < 9) {
    date.setMonth(6);
  } else if (8 < month && month < 11) {
    date.setMonth(9);
  }
  date.setDate(1);
  return timeFormat(date);
}
//获取当年第一天
function getFirstDayOfYear(date) {
  date.setDate(1);
  date.setMonth(0);
  return timeFormat(date);
}


//随机渐变色  ---  开始颜色，结束颜色  一共多少个数组
function gradientColor(startColor, endColor, step) {
  startRGB = colorRgb(startColor); //转换为rgb数组模式
  startR = startRGB[0];
  startG = startRGB[1];
  startB = startRGB[2];

  endRGB = colorRgb(endColor);
  endR = endRGB[0];
  endG = endRGB[1];
  endB = endRGB[2];

  sR = (endR - startR) / step; //总差值
  sG = (endG - startG) / step;
  sB = (endB - startB) / step;

  var colorArr = [];

  for (var i = 0; i < step; i++) {
    //计算每一步的hex值
    var hex = this.colorHex(
      "rgb(" +
      parseInt(sR * i + startR) +
      "," +
      parseInt(sG * i + startG) +
      "," +
      parseInt(sB * i + startB) +
      ")"
    );

    colorArr.push(hex);
  }
  return colorArr;
}

// 将hex表示方式转换为rgb表示方式(这里返回rgb数组模式)
function colorRgb(sColor) {
  var reg = /^#([0-9a-fA-f]{3}|[0-9a-fA-f]{6})$/;
  var sColor = sColor.toLowerCase();
  if (sColor && reg.test(sColor)) {
    if (sColor.length === 4) {
      var sColorNew = "#";
      for (var i = 1; i < 4; i += 1) {
        sColorNew += sColor
          .slice(i, i + 1)
          .concat(sColor.slice(i, i + 1));
      }
      sColor = sColorNew;
    }
    //处理六位的颜色值
    var sColorChange = [];
    for (var i = 1; i < 7; i += 2) {
      sColorChange.push(parseInt("0x" + sColor.slice(i, i + 2)));
    }
    return sColorChange;
  } else {
    return sColor;
  }
}

// 将rgb表示方式转换为hex表示方式
function colorHex(rgb) {
  var _this = rgb;
  var reg = /^#([0-9a-fA-f]{3}|[0-9a-fA-f]{6})$/;
  if (/^(rgb|RGB)/.test(_this)) {
    var aColor = _this.replace(/(?:(|)|rgb|RGB)*/g, "").split(",");
    var strHex = "#";
    for (var i = 0; i < aColor.length; i++) {
      var hex = Number(aColor[i]).toString(16);
      hex = hex < 10 ? 0 + "" + hex : hex; // 保证每个rgb的值为2位
      if (hex === "0") {
        hex += hex;
      }
      strHex += hex;
    }
    if (strHex.length !== 7) {
      strHex = _this;
    }
    return strHex;
  } else if (reg.test(_this)) {
    var aNum = _this.replace(/#/, "").split("");
    if (aNum.length === 6) {
      return _this;
    } else if (aNum.length === 3) {
      var numHex = "#";
      for (var i = 0; i < aNum.length; i += 1) {
        numHex += aNum[i] + aNum[i];
      }
      return numHex;
    }
  } else {
    return _this;
  }
}


//数组拆分数组  array原数组    subGroupLength 小数组长度
function group(array, subGroupLength) {
  let index = 0;
  let newArray = [];
  while (index < array.length) {
    newArray.push(array.slice(index, (index += subGroupLength)));
  }
  return newArray;
}

var GradationColors = [
  "#C0E6C0", "#80CC80",
  "#FFCCFF", "#FF99FF",
  "#FFCCCC", "#FF9999",
  "#99B3FF", "#3366FF",
  "#FFB380", "#FF6600",
  "#99CCFF", "#3399FF",
  "#B3FFB3", "#66FF66",
  "#9999E6", "#3333CC",
  "#FFBFFF", "#FF00FF",
  "#E6F2BF", "#99CC00",
  "#D9D9FF", "#6666FF",
  "#D9BFCC", "#8C4066",
  "#CCBFFF", "#6640FF",
  "#E6B3FF", "#CC66FF",
  "#F2D9D9", "#CC6666",
  "#BFD9D9", "#408C8C",
  "#D9E6BF", "#8CB340",
  "#FFBFCC", "#FF0033",
  "#F2FFBF", "#CCFF00",
  "#F2E6FF", "#CC99FF",
  "#D9F2F2", "#66CCCC",
  "#FFE6D9", "#FF9966",
  "#F2CCCC", "#CC3333",
  "#CCFFE6", "#66FFB3",
  "#FFCCFF", "#FF66FF",
  "#F2E6CC", "#CC9933",
  "#F2E6E6", "#CC9999",
  "#E6BFF2", "#CC80E6",
  "#FFE6CC", "#FFB366",
  "#FF9999", "#FF3333"
];
var GradationColors1 = [
  "#80CC80",
  "#FF99FF",
  "#FF9999",
  "#3366FF",
  "#FF6600",
  "#3399FF",
  "#66FF66",
  "#3333CC",
  "#FF00FF",
  "#99CC00",
  "#6666FF",
  "#8C4066",
  "#6640FF",
  "#CC66FF",
  "#CC6666",
  "#408C8C",
  "#8CB340",
  "#FF0033",
  "#CCFF00",
  "#CC99FF",
  "#66CCCC",
  "#FF9966",
  "#CC3333",
  "#66FFB3",
  "#FF66FF",
  "#CC9933",
  "#CC9999",
  "#CC80E6",
  "#FFB366",
  "#FF3333"
];